package es.us.isa.jdbl.data;

import java.util.LinkedList;
import java.util.List;

import javax.persistence.EntityManager;

import es.us.isa.jdbl.domain.Member;
import es.us.isa.jdbl.domain.Project;
import es.us.isa.jdbl.domain.biblio.BookChapter;
import es.us.isa.jdbl.domain.biblio.Contributors;

public class ProjectDAO {
	
	public static List<Project> getAll(EntityManager em){
		
		return em.createQuery("from Project").getResultList();
	}
	
	public static List<Project> getByAuthor(EntityManager em, Member m){
		List<Project> lista = em.createQuery("from Project").getResultList();
		List<Project> resultado = new LinkedList<Project>();
		
		for (Project aux:lista){
			/*Comprobar que sea miembro investigador*/
			for (Member c:aux.getResearchStaff() ){
				if(c != null){ 
						if( c.equals(m)){					
							resultado.add(aux);
					}
				}
			}
			
			/*Comprobar que sea miembro investigador*/
			for (Member c:aux.getTechnicalStaff() ){
				if(c != null){ 
					if( c.equals(m)){					
						resultado.add(aux);
				}
			}
			}
			
			Member c = aux.getCoordinator();
			if(c != null){ 
				if( c.equals(m)){					
					resultado.add(aux);
			}
		}
			
			c = aux.getCoordinatorUnivSevilla();
			if(c != null){ 
				if( c.equals(m)){					
					resultado.add(aux);
			}
		}
			
		}
		return resultado;
		
		}

}